import json

from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from django.views import View

from app01.models import User, Book
from django.core import serializers
from django.core.paginator import Paginator


# Create your views here.
def add(request):
    if request.method == "GET":
        return render(request, "num.html")
    if request.method == "POST":
        num1 = request.POST.get("num1")
        num2 = request.POST.get("num2")
        return HttpResponse(int(num1) + int(num2))


def upload_file(request):
    if request.method == "GET":
        return render(request, "upload_file.html")
    if request.method == "POST":
        get_files = request.FILES.get("myfile")
        print(type(get_files), get_files)
        # print(get_files.name)
        with open(get_files.name, "wb") as f:
            for line in get_files.chunks():
                f.write(line)

        from django.core.files.uploadedfile import InMemoryUploadedFile
        return HttpResponse("ok")


def ajax_file(request):
    if request.method == "GET":
        return render(request, "ajax_file.html")

    if request.method == "POST":
        get_files = request.FILES.get("my_file")
        with open(get_files.name, "wb") as f:
            for line in get_files.chunks():
                f.write(line)
        return HttpResponse("ok")


def jsons(request):
    if request.method == "GET":
        return render(request, "jsons.html")
    if request.method == "POST":
        print(request.POST, type(request.POST))
        print(request.body)
        res = {"status": "100", "age": 100}
        # return HttpResponse(json.dumps(res))
        # return JsonResponse(res)
        return HttpResponse(json.dumps(res))


def user_list(request):
    if request.method == "GET":
        return render(request, "user_list.html")

    if request.method == "POST":
        users = User.objects.all()
        print(users)
        ret = serializers.serialize("json", users)
        # [{"model": "app01.user", "pk": 1, "fields": {"name": "xiong", "pwd": "123"}}, {
        print(ret)
        return HttpResponse(ret)


class Page_lists(View):
    def get(self, request):
        # x = [Book(name="python-{}".format(i), pirce=i) for i in range(100)]
        # Book.objects.bulk_create(x,10)
        book_list = Book.objects.all()
        paginator = Paginator(book_list, 10)  # 列表，每列10个

        try:
            page_num = paginator.validate_number(request.GET.get("page_num"))
        except Exception:
            page_num = 1
        get_page = paginator.page(page_num)


        page_paginator_num = paginator.num_pages

        # 1、判断 总页码 是否大于11行
        if page_paginator_num > 11:
            # 获取标签减 5个或者等于1 范围就是 1到 11
            if page_num - 5 <= 1:
                page_range = range(1, 11)
            # 获取标签 加5 大于获取的总数 就是 左
            # 总数20 ， 获取是14+5  那就是 14-10， 4,13
            elif page_num + 5 > page_paginator_num:
                page_range = range(page_paginator_num - 10, page_paginator_num + 1)
            else:
                page_range = range(page_num - 5, page_num + 5)
        else:
            # 1、有多少显示多少
            page_range = paginator.page_range

        return render(request, "page_list.html", {"get_page": get_page,
                                                  "page_num": page_num,
                                                  "page_range": page_range})

        # 获取行数，页面数，以及queryset对象
        # print(paginator.count)                # 共100行
        # print(paginator.get_page(3))          # <Page 1 of 10>, 没有做什么检验
        # print(paginator.num_pages)            # 分成10页
        # print(paginator.page(3))              # 与get_page相同， 后端做了很多比较
        # print(paginator.page_range)           # range(1, 11)
        # print(paginator.object_list)          # <QuerySet [<Book: name: python-0>, ...
        # get_page = paginator.page(3)        # get_page与page一样
        # print(get_page.object_list)         # page与object_list配合取出对象的queryset对象
        #
        # print(paginator.per_page)           # per_page 传递的每页数
        # # print(paginator.validate_number("abc"))  # 分页器做的检验
        # print(get_page.has_next())          # 是否有下一页
        # print(get_page.has_previous())      # 是否有上一页
        # print(get_page.next_page_number())  # 获取下一页的页码
        # print(get_page.previous_page_number())  # 获取上一页的页码

        # return render(request, "page_list.html")

    def post(self, request):
        pass
